package com.roc.web.action.setting;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.roc.service.setting.ISettflagService;
import com.roc.util.ApplicationContextUtils;
import com.roc.util.constant.AuditingType;
import com.roc.web.timer.CalcuUtil;

import java.io.BufferedReader;                                                                      
import java.io.BufferedWriter;
import java.io.IOException;                                                                         
import java.io.InputStream;                                                                         
import java.io.InputStreamReader;                                                                   
import java.io.OutputStreamWriter;                                                                  
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;                                                                                
import java.net.URLConnection;      

@Component                                                                                                    
public class HttpContect {  
	
	@Autowired
	ISettflagService settflagService;
                                                                                                    
    public static void testPost() throws IOException {                                              
                                                                                                    
        /**                                                                                         
         * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如： // Using   
         *  java.net.URL and //java.net.URLConnection                                               
         */                                                                                         
        URL url = new URL("http://192.168.10.223:7003/dypay/SdpayAction");                           
       // URLConnection connection = url.openConnection();    
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        /**                                                                                         
         * 然后把连接设为输出模式。URLConnection通常作为输入来使用，比如下载一个Web页。             
         * 通过把URLConnection设为输出，你可以把数据向你个Web页传送。下面是如何做：                 
         */                                                                                         
        connection.setDoOutput(true);   
        connection.setDoInput(true);
        /**                                                                                         
         * 最后，为了得到OutputStream，简单起见，把它约束在Writer并且放入POST信息中，例如： ...     
         */                                                                                         
//        OutputStreamWriter out = new OutputStreamWriter(connection                                  
//                .getOutputStream(), "8859_1");
        PrintWriter out = new PrintWriter(
				new BufferedWriter(new OutputStreamWriter(
						connection.getOutputStream(), "UTF-8")));
       // settflagService.querySettingItemList();
        JSONArray array = null;
		 array = new JSONArray();
		 
			 JSONObject obj=new JSONObject();
			 obj.put("id", "1");
			 obj.put("serial", "S00013");
			 obj.put("accountno", "123456789"); 
			 obj.put("accountname", "老曹");
			 obj.put("amt", "123456"); 
			 obj.put("bankname", "中国建设银行徐汇支行");
			 obj.put("banklinkno", "3021234"); 
			 obj.put("settledate", "20140821");
			 obj.put("settletime", "201408");
			 obj.put("delval", "1"); 
			 array.add(obj);
			 JSONObject obj1=new JSONObject();
			 obj1.put("id", "2");
			 obj1.put("serial", "S00014");
			 obj1.put("accountno", "123456888"); 
			 obj1.put("accountname", "曹二货");
			 obj1.put("amt", "10001"); 
			 obj1.put("bankname", "中国工商银行徐汇支行");
			 obj1.put("banklinkno", "3021235"); 
			 obj1.put("settledate", "20140821");
			 obj1.put("settletime", "140821");
			 obj1.put("delval", "1"); 
			 array.add(obj1);
			 
		 
		 System.out.print(array.toString());
		 String json = array.toString();
		 out.print(json); //post的关键所在！   
        // remember to clean up                                                                     
        out.flush();                                                                                
        out.close();                                                                                
        /**                                                                                         
         * 这样就可以发送一个看起来象这样的POST：                                                   
         * POST /jobsearch/jobsearch.cgi HTTP 1.0 ACCEPT:                                           
         * text/plain Content-type: application/x-www-form-urlencoded                               
         * Content-length: 99 username=bob password=someword                                        
         */           
        String result = "";
        // 一旦发送成功，用以下方法就可以得到服务器的回应：                                         
        BufferedReader in = new BufferedReader(new InputStreamReader(
        		connection.getInputStream(), "UTF-8"));
		String line;
		while ((line = in.readLine()) != null) {
			result += line;
		}
		in.close();                                                             
       System.out.println(result);                                                           
    }                                                                                               
                                                                                                    
    public static void main(String[] args) throws IOException {                                     
                                                                                                    
        testPost();  
//        JSONArray array = null;
//		 array = new JSONArray();
//		 for(int i=0;i<4;i++)
//		 { 
//			 JSONObject obj=new JSONObject();
//			 obj.put("N", "1234");
//			 obj.put("name", "song"); 
//			 array.add(obj);
//		 }
		 
//		 String ss = "[{\"N\":\"1234\",\"name\":\"song\"},{\"N\":\"1234\",\"name\":\"song1\"},{\"N\":\"1234\",\"name\":\"song2\"},{\"N\":\"1234\",\"name\":\"song3\"}]";
//		 JSONArray ar  = new JSONArray().fromObject(ss);	
//		 for (int i = 0; i < ar.size(); i++) {			
//			 JSONObject iObj = ar.getJSONObject(i);			
//		 System.out.println(iObj.get("name"));		
//		 }
		
		 
		 
		                                                                     
    }                                                                                               
                                                                                                    
}                                                                                                   

